package it.agm.fanta.fragment;

import it.agm.fanta.FantaserviceApp;
import it.agm.fanta.R;
import it.agm.fanta.adapter.GiornataCalendarioAdapter;
import it.agm.fanta.model.Partita;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


public class CalendarioFragment extends Fragment {
	private static final int PAGINATOR_ADAPTER = 1;

	public static final String ARG_PLANET_NUMBER = "planet_number";

	private ListView listview;
    private TextView title;
     
    private ArrayList<String> data;
    ArrayAdapter<String> sd;
     
    public int TOTAL_LIST_ITEMS = 0;
    public int NUM_ITEMS_PAGE   = 0;
    private int noOfBtns;
    private Button[] btns;
    LayoutInflater inflaterFragment;
	public CalendarioFragment() {
		// Empty constructor required for fragment subclasses
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_calendar_paginator, container,false);
		LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.calendarListView);
		title    = (TextView)layout.findViewById(R.id.titlePagination);
		if(FantaserviceApp.partite != null && FantaserviceApp.partite.getPartite() != null && !FantaserviceApp.partite.getPartite().isEmpty()  && FantaserviceApp.lega != null){
			inflaterFragment = inflater;
			listview = (ListView)rootView.findViewById(R.id.listPagination);
		   
			TOTAL_LIST_ITEMS = FantaserviceApp.partite.getPartite().size();
			NUM_ITEMS_PAGE = FantaserviceApp.lega.getSquadreiscritte() /2 ;
			data = new ArrayList<String> ();
//			for(int j=0 ; j<App.partite.length ; j++){
//				Partita partita = App.partite[j];
//				data.add(partita.getSquadracasa() +" "+partita.getGolsquadracasa() +"-"+ partita.getGolsquadratrasferta() +" "+partita.getSquadratrasferta());
//			}
//			ArrayList<Partita> list = new ArrayList<Partita>(Arrays.asList(App.partite));
//			GiornataCalendarioAdapter adapter = new GiornataCalendarioAdapter(getActivity(), R.layout.row_quadra_classifica,list);

//			listView.setAdapter(adapter);
		    Btnfooter(rootView);
		    if(FantaserviceApp.lega.getGiornatacorrente()>0){
			    loadList(FantaserviceApp.lega.getGiornatacorrente() - PAGINATOR_ADAPTER,rootView);
	            CheckBtnBackGroud(FantaserviceApp.lega.getGiornatacorrente() - PAGINATOR_ADAPTER );
		    }
		}else{
			 title.setText("Calendario non disponibile");
		}
		return rootView;
	}
	
	private void Btnfooter(View rootView){
        int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;
        val = val==0?0:1;
        noOfBtns=TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE+val;
         
        LinearLayout linearLayout = (LinearLayout)rootView.findViewById(R.id.btnLayPagination);
         
        btns = new Button[noOfBtns];
         
        for(int i=0;i<noOfBtns;i++) {
            btns[i] = new Button(rootView.getContext());
//            btns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
            btns[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.background_calendar_botton));
            btns[i].setText(""+(i+1));
             
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            linearLayout.addView(btns[i], params);
           
            final int j = i;
            btns[j].setOnClickListener(new OnClickListener() {
                 
                public void onClick(View v) 
                {
                    loadList(j,v);
                    CheckBtnBackGroud(j);
                }
            }); 
        }
         
    }
	
	/**
     * Method for Checking Button Backgrounds
     */
    private void CheckBtnBackGroud(int index){
        title.setText("Giornate "+(index+1)+" di "+noOfBtns);
        if(index <= btns.length){
        	btns[index].setSelected(true);
        }
        for(int i=0;i<noOfBtns;i++){
        	if(i!=index){
        		btns[i].setSelected(false);
        	}

        }
         
    }
     
    /**
     * Method for loading data in listview
     * @param number
     */
    private void loadList(int number,View view) {
        ArrayList<String> sort = new ArrayList<String>();
         
        int start = number * NUM_ITEMS_PAGE;
//        for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++){
//            if(i<data.size()) {
//                sort.add(data.get(i));
//            }
//            else{
//                break;
//            }

        ArrayList<Partita> list = new ArrayList<Partita>();
        for(int j=start;j<(start)+NUM_ITEMS_PAGE;j++){
        	
        	if(j< FantaserviceApp.partite.getPartite().size()) {	
        	 Partita partita=FantaserviceApp.partite.getPartite().get(j);
        	 list.add(partita);
        	 
        	}else{
                 break;
             } 
		}
        GiornataCalendarioAdapter adapter = new GiornataCalendarioAdapter(getActivity(), R.layout.row_squadra_classifica,list);
        listview.setAdapter(adapter);
        
        
//        sd = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_list_item_1, sort);
//        listview.setAdapter(sd);
    }
}
